#include "MenuPage.h"

#include "../Settings.h"
#include "../Input/Input.h"

#include "MoveMenu.h"

#include <GL/gl.h>

MenuPage::MenuPage()
 :
    page_top(0.75f), page_bot(0.0f),
    page_left(0.0f), page_right(1.0f),
    title_left(0.1f), title_right(0.9f),
    title_top(0.725f), title_bot(0.625f),
    items_top(0.61f), items_bot(0.01f),
    items_left(0.2f), items_right(0.8f),
    mouse_pos(Input::instance().getMousePosition(true)),
    title(""),
    current_item(-1),
    current_top_item(-1),
    num_items(0),
    num_regions(6),
    menu_font(Settings::instance().getStringValue("menu","font_file"), 0.05f, 0.035f)
{
    int i, num = num_regions-1;
    i = num;
    for (; i >= 0; i--) {
        float base = items_bot + 0.1f * i;
//        select_regions[num-i] = Vector2D(base + 0.095f,base + 0.005f);
//        display_regions[num-i] = Vector2D(base + 0.09f,base + 0.01f);
        select_regions.push_back(Vector2D(base + 0.095f,base + 0.005f));
        display_regions.push_back(Vector2D(base + 0.09f,base + 0.01f));
    }
    move_up = new MoveMenu(true, this);
    move_down = new MoveMenu(false, this);
//    std::cerr << "init menupage\n";
}

MenuPage::~MenuPage () {
}

void MenuPage::move(bool direction) {
    if (!direction) {
        //move up
        if (current_top_item + 6 < num_items) {
            current_top_item++;
            current_item++;
        }
        if (current_item < current_top_item) {
            current_item = current_top_item;
        }
    } else {
        //move down
        if (current_top_item > 0) {
            current_top_item--;
            current_item--;
        }
        if (current_item > current_top_item + 5) {
            current_item = current_top_item + 5;
        }
    }
}

void MenuPage::render() {
    // render 
    //render the title in the top 1/5th of the screen
    //
    
//    std::cerr << "redner menupage begin\n";
    glDisable(GL_TEXTURE_2D);

    //just render square for now
    glBegin(GL_QUADS);

    glColor4f(0.0f, 0.5f, 0.0f, 1.0f);
    glVertex3f(title_left, title_top, 0.0f);
    glVertex3f(title_left, title_bot, 0.0f);

    glColor4f(0.0f, 0.5f, 0.0f, 1.0f);
    glVertex3f(title_right, title_bot, 0.0f);
    glVertex3f(title_right, title_top, 0.0f);

    glEnd();

    glPushMatrix();
    glColor3f(0.9f, 0.9f, 0.7f);
    glTranslatef(0.35f, 0.675f, 0.0f);
    menu_font.renderString(title);
    glPopMatrix();

    // render each menu item in a given space in
    // the bottom 4/5ths of the screen
    //
    // scrolling?

//    std::map<int,MenuItem*>::iterator it;
//    std::map<int,Vector2D>::iterator rit;
    std::vector<MenuItem*>::iterator it = items.begin();
    std::vector<Vector2D>::iterator rit = display_regions.begin();
    int i = 0;
    
    //cycle through current_top_item times
    for (   ;
            i < current_top_item;
            i++) {
        ++it;
    }

    // if the first is not the top item
    // i.e. current_top_item != 0

//    std::cerr << "redner menupage after title\n";
    for (   ;
            rit != display_regions.end() && it != items.end() && i != num_items;
            ++rit, ++it, ++i) {
        // regions are the selecting boundaries and not the actual drawing sizes
        Vector2D tmp = *rit;
        (*it)->render(tmp.x, tmp.y, items_left, items_right, i == current_item);
    }
//    std::cerr << "render menupage end\n";
}

void MenuPage::activate() {
    MenuItem* it = items[current_item];
    if (it) it->activate();
}

void MenuPage::update() {
//    std::cerr << "update menupage begin\n";
    if (Input::instance().isKeyDownAndClear("menu_scroll_up")) {
        move(true);
    }
    if (Input::instance().isKeyDownAndClear("menu_scroll_down")) {
        move(false);
    }
    Vector2D new_mouse_pos = Input::instance().getMousePosition(true);
    if (new_mouse_pos != mouse_pos) {
       // select the menu item underneath or nearest the cursor
        select(new_mouse_pos.x, new_mouse_pos.y);
        // and then clear the move up and move down keys
    } else {
        if (Input::instance().isKeyDownAndClear("menu_up")) {
            --current_item;
        }
        if (Input::instance().isKeyDownAndClear("menu_down")) {
            ++current_item;
        }
        if (current_item < 0) {
            current_item = 0;
        }
        if (current_item >= (int)items.size()) {
            current_item = items.size() - 1;
        }
        if (current_item < current_top_item) {
            current_top_item = current_item;
        }
        if (current_item > current_top_item + 5) {
            current_top_item = current_item - 5;
        }
    }
//    std::cerr << "update menupage end\n";
    mouse_pos = new_mouse_pos;
}

void MenuPage::select(float x, float y) {
    std::vector<Vector2D>::iterator it;
    //std::map<int,Vector2D>::iterator it;
    int i = 0;
    for (it = select_regions.begin();
         it != select_regions.end() && i != num_items;
         ++i, ++it) {
        Vector2D vec = Vector2D(*it);
        if (y <= vec.x && y >= vec.y) {
            current_item = current_top_item + i;
        }
    }
}
